<?php
if (!extension_loaded("phalcon")) {
    die("Must be install the ext-phalcon to run");
}
$di = new Phalcon\DI\FactoryDefault();
if (php_sapi_name() != "cli") {
    $debug = new Phalcon\Debug();
    $debug->listen();
}

include_once "vendor/autoload.php";

define('SRC_DIR', realpath(__DIR__ . '/../../') . '/');
define("PHP_THRIFT_DIR", SRC_DIR . 'git.apache.org/thrift.git/lib/php/lib');
//PhalconLoader
{
    $loader = new Phalcon\Loader();
    $loader->registerNamespaces([
        'Thrift\ClassLoader' => PHP_THRIFT_DIR . '/Thrift/ClassLoader/',
        'Application\Services' => 'application/services/',
        'Application\Models' => 'application/models/',
        'Application\Handlers' => 'application/handlers/',
        'Application\Controllers' => "application/controllers/"
    ], true);
    $loader->register();
}

//thriftLoader
if(false){
    $thriftLoader = new \Thrift\ClassLoader\ThriftClassLoader();
    # 加载thrift
    $thriftLoader->registerNamespace('Thrift', PHP_THRIFT_DIR);
    # 加载自己写的thrift文件编译的类文件和数据定义
    $thriftLoader->registerDefinition('BlogServices', SRC_DIR . '/gen-php');
    $thriftLoader->register();
}

$di->set('db', function () {
    return new \Phalcon\Db\Adapter\Pdo\Mysql([
        "host" => "qdm19508111.my3w.com",
        "username" => "qdm19508111",
        "password" => "ccb159781",
        "dbname" => "qdm19508111_db",
        "charset" => "utf8",
        'port' =>"3306",
        "options" => [  //初始化执行的选项
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
            PDO::ATTR_CASE => PDO::CASE_LOWER   //全小写
        ]
    ]);
});

$di->set("voltService", function ($view, $di) {
    $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di);
    $volt->setOptions([
        "compileAlways"=>true,
        "compiledExtension" => ".compiled.php",
    ]);

    return $volt;
});

$di->set('view', function () {
    $view = new Phalcon\Mvc\View();
    $view->setViewsDir("application/views/");
    $view->registerEngines([".volt" => "voltService"]);
    return $view;
}, true);

$di->set('url', function () {
    $url = new \Phalcon\Mvc\Url();
    $domain = "http://" . $_SERVER['HTTP_HOST'] . '/';
    $url->setBaseUri($domain . "index.php?_url=");
    $url->setStaticBaseUri($domain);
    return $url;
});

$di->set('router', function () {
    $router = new Phalcon\Mvc\Router(true);
    //设置基本的Controllers命名空间
    $router->setDefaultNamespace("Application\\Controllers");
    return $router;
});


function dd(...$variables)
{
    $dump = new Phalcon\Debug\Dump();
    echo $dump->variables(...$variables);
}